package com.fr.report.core;

import com.fr.base.ColumnRow;
import com.fr.report.CellElement;
import com.fr.report.Report;
import com.fr.report.cellElement.CellExpandAttr;
import com.fr.report.cellElement.core.DSColumn;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SheetUtils
{
  private static CellElement getUpParent(Report paramReport, CellElement paramCellElement)
  {
    if (paramCellElement.getCellExpandAttr().getUpParentColumnRow() != null)
      return paramReport.getCellElement(paramCellElement.getCellExpandAttr().getUpParentColumnRow().column, paramCellElement.getCellExpandAttr().getUpParentColumnRow().row);
    return null;
  }

  private static boolean isUpCycle(Report paramReport, CellElement paramCellElement)
  {
    int i = 0;
    HashSet localHashSet = new HashSet();
    int j = 1;
    localHashSet.add(paramCellElement);
    for (CellElement localCellElement = getUpParent(paramReport, paramCellElement); localCellElement != null; localCellElement = getUpParent(paramReport, localCellElement))
    {
      localHashSet.add(localCellElement);
      if (localHashSet.size() != ++j)
      {
        i = 1;
        break;
      }
    }
    return i;
  }

  private static CellElement getLeftParent(Report paramReport, CellElement paramCellElement)
  {
    if (paramCellElement.getCellExpandAttr().getLeftParentColumnRow() != null)
      return paramReport.getCellElement(paramCellElement.getCellExpandAttr().getLeftParentColumnRow().column, paramCellElement.getCellExpandAttr().getLeftParentColumnRow().row);
    return null;
  }

  private static boolean isLeftCycle(Report paramReport, CellElement paramCellElement)
  {
    int i = 0;
    HashSet localHashSet = new HashSet();
    int j = 1;
    localHashSet.add(paramCellElement);
    for (CellElement localCellElement = getLeftParent(paramReport, paramCellElement); localCellElement != null; localCellElement = getLeftParent(paramReport, localCellElement))
    {
      localHashSet.add(localCellElement);
      if (localHashSet.size() != ++j)
      {
        i = 1;
        break;
      }
    }
    return i;
  }

  public static void calculateDefaultParent(Report paramReport)
  {
    Object localObject2;
    Object localObject3;
    Object localObject4;
    int i1;
    int i3;
    int i5;
    int i6;
    if (paramReport == null)
      return;
    Object localObject1 = new CellElement[paramReport.getColumnCount()];
    CellElement[] arrayOfCellElement = new CellElement[paramReport.getColumnCount()];
    int i = 0;
    int j = paramReport.getRowCount();
    while (i < j)
    {
      localObject2 = paramReport.getRow(i);
      do
      {
        while (true)
        {
          if (!(((Iterator)localObject2).hasNext()))
            break label509;
          localObject3 = (CellElement)((Iterator)localObject2).next();
          if (localObject3 != null)
            break;
        }
        localObject4 = ((CellElement)localObject3).getCellExpandAttr();
        if (localObject4 == null)
        {
          localObject4 = new CellExpandAttr();
          ((CellExpandAttr)localObject4).setDirection(2);
          ((CellElement)localObject3).setCellExpandAttr((CellExpandAttr)localObject4);
        }
        if (((CellExpandAttr)localObject4).isUpParentDefault())
        {
          Object localObject5 = null;
          i1 = ((CellElement)localObject3).getColumn();
          int i2 = 0;
          i5 = ((CellElement)localObject3).getColumnSpan();
          while (i2 < i5)
          {
            localObject5 = localObject1[(i2 + i1)];
            if ((localObject5 != null) && (localObject5 != localObject3))
              break;
            ++i2;
          }
          ((CellExpandAttr)localObject4).setUpParentColumnRow(((localObject5 == null) || (localObject5 == localObject3)) ? null : ColumnRow.valueOf(localObject5.getColumn(), localObject5.getRow()));
          if (isUpCycle(paramReport, (CellElement)localObject3))
          {
            ((CellExpandAttr)localObject4).setUpParentColumnRow(null);
            localObject1 = arrayOfCellElement;
            CellElement localCellElement3 = null;
            i5 = 0;
            i6 = ((CellElement)localObject3).getColumnSpan();
            while (i5 < i6)
            {
              localCellElement3 = arrayOfCellElement[(i5 + i1)];
              if ((localCellElement3 != null) && (localCellElement3 != localObject3))
                break;
              ++i5;
            }
            ((CellExpandAttr)localObject4).setUpParentColumnRow(((localCellElement3 == null) || (localCellElement3 == localObject3)) ? null : ColumnRow.valueOf(localCellElement3.getColumn(), localCellElement3.getRow()));
          }
        }
        if (!(isUpCycle(paramReport, (CellElement)localObject3)))
        {
          k = 0;
          i1 = ((CellElement)localObject3).getColumnSpan();
          while (k < i1)
          {
            arrayOfCellElement[(k + localObject3.getColumn())] = localObject1[(k + localObject3.getColumn())];
            ++k;
          }
        }
        if (((CellExpandAttr)localObject4).getDirection() == 1)
        {
          k = ((CellElement)localObject3).getColumn();
          i1 = 0;
          i3 = ((CellElement)localObject3).getColumnSpan();
          while (true)
          {
            if (i1 >= i3);
            if (!(isUpCycle(paramReport, (CellElement)localObject3)))
              localObject1[(i1 + k)] = localObject3;
            ++i1;
          }
        }
      }
      while ((((CellExpandAttr)localObject4).isUpParentDefault()) || (((CellExpandAttr)localObject4).getUpParentColumnRow() != null));
      int k = ((CellElement)localObject3).getColumn();
      i1 = 0;
      i3 = ((CellElement)localObject3).getColumnSpan();
      while (true)
      {
        if (i1 >= i3);
        localObject1[(i1 + k)] = null;
        ++i1;
      }
      label509: ++i;
    }
    localObject1 = new CellElement[paramReport.getRowCount()];
    arrayOfCellElement = new CellElement[paramReport.getRowCount()];
    i = 0;
    j = paramReport.getColumnCount();
    while (i < j)
    {
      localObject2 = paramReport.getColumn(i);
      do
      {
        while (true)
        {
          if (!(((Iterator)localObject2).hasNext()))
            break label1018;
          localObject3 = (CellElement)((Iterator)localObject2).next();
          if (localObject3 != null)
            break;
        }
        localObject4 = ((CellElement)localObject3).getCellExpandAttr();
        if (localObject4 == null)
        {
          localObject4 = new CellExpandAttr();
          ((CellExpandAttr)localObject4).setDirection(2);
          ((CellElement)localObject3).setCellExpandAttr((CellExpandAttr)localObject4);
        }
        if (((CellExpandAttr)localObject4).isLeftParentDefault())
        {
          Object localObject6 = null;
          i1 = ((CellElement)localObject3).getRow();
          i3 = 0;
          i5 = ((CellElement)localObject3).getRowSpan();
          while (i3 < i5)
          {
            localObject6 = localObject1[(i3 + i1)];
            if ((localObject6 != null) && (localObject6 != localObject3))
              break;
            ++i3;
          }
          ((CellExpandAttr)localObject4).setLeftParentColumnRow(((localObject6 == null) || (localObject6 == localObject3)) ? null : ColumnRow.valueOf(localObject6.getColumn(), localObject6.getRow()));
          if (isLeftCycle(paramReport, (CellElement)localObject3))
          {
            ((CellExpandAttr)localObject4).setLeftParentColumnRow(null);
            localObject1 = arrayOfCellElement;
            CellElement localCellElement4 = null;
            i5 = 0;
            i6 = ((CellElement)localObject3).getRowSpan();
            while (i5 < i6)
            {
              localCellElement4 = arrayOfCellElement[(i5 + i1)];
              if ((localCellElement4 != null) && (localCellElement4 != localObject3))
                break;
              ++i5;
            }
            ((CellExpandAttr)localObject4).setLeftParentColumnRow(((localCellElement4 == null) || (localCellElement4 == localObject3)) ? null : ColumnRow.valueOf(localCellElement4.getColumn(), localCellElement4.getRow()));
          }
        }
        if (!(isLeftCycle(paramReport, (CellElement)localObject3)))
        {
          l = 0;
          i1 = ((CellElement)localObject3).getRowSpan();
          while (l < i1)
          {
            arrayOfCellElement[(l + localObject3.getRow())] = localObject1[(l + localObject3.getRow())];
            ++l;
          }
        }
        if (((CellExpandAttr)localObject4).getDirection() == 0)
        {
          l = ((CellElement)localObject3).getRow();
          i1 = 0;
          i4 = ((CellElement)localObject3).getRowSpan();
          while (true)
          {
            if (i1 >= i4);
            if (!(isLeftCycle(paramReport, (CellElement)localObject3)))
              localObject1[(i1 + l)] = localObject3;
            ++i1;
          }
        }
      }
      while ((((CellExpandAttr)localObject4).isLeftParentDefault()) || (((CellExpandAttr)localObject4).getLeftParentColumnRow() != null));
      int l = ((CellElement)localObject3).getRow();
      i1 = 0;
      int i4 = ((CellElement)localObject3).getRowSpan();
      while (true)
      {
        if (i1 >= i4);
        localObject1[(i1 + l)] = null;
        ++i1;
      }
      label1018: ++i;
    }
    Iterator localIterator = paramReport.cellIterator();
    while (true)
    {
      CellElement localCellElement1;
      CellElement localCellElement2;
      do
      {
        do
        {
          while (true)
          {
            if (!(localIterator.hasNext()))
              return;
            localCellElement1 = (CellElement)localIterator.next();
            if (localCellElement1 != null)
              break;
          }
          localObject2 = localCellElement1.getCellExpandAttr();
          localObject3 = ((CellExpandAttr)localObject2).getLeftParentColumnRow();
          if (localObject3 != null)
          {
            localObject4 = paramReport.getCellElement(((ColumnRow)localObject3).column, ((ColumnRow)localObject3).row);
            if (localObject4 != null)
              setAreaDefaultLeftParent(paramReport, (CellElement)localObject4, localCellElement1);
          }
          localObject4 = ((CellExpandAttr)localObject2).getUpParentColumnRow();
        }
        while (localObject4 == null);
        localCellElement2 = paramReport.getCellElement(((ColumnRow)localObject4).column, ((ColumnRow)localObject4).row);
      }
      while (localCellElement2 == null);
      setAreaDefaultUpParent(paramReport, localCellElement2, localCellElement1);
    }
  }

  private static void setAreaDefaultLeftParent(Report paramReport, CellElement paramCellElement1, CellElement paramCellElement2)
  {
    int i = paramCellElement1.getRow();
    int j = paramCellElement1.getRow() + paramCellElement1.getRowSpan() - 1;
    int k = paramCellElement1.getColumn();
    int l = paramCellElement2.getRow();
    int i1 = paramCellElement2.getRow() + paramCellElement2.getRowSpan() - 1;
    if (j == i1)
    {
      if (i <= l)
        return;
      setDefaultLeftParent(ColumnRow.valueOf(k, i), paramReport, l, i - 1);
    }
    else if (j < l)
    {
      setDefaultLeftParent(ColumnRow.valueOf(k, i), paramReport, j + 1, l);
    }
    else if (i1 < i)
    {
      setDefaultLeftParent(ColumnRow.valueOf(k, i), paramReport, i1, i - 1);
    }
  }

  private static void setAreaDefaultUpParent(Report paramReport, CellElement paramCellElement1, CellElement paramCellElement2)
  {
    int i = paramCellElement1.getRow();
    int j = paramCellElement1.getColumn();
    int k = paramCellElement1.getColumn() + paramCellElement1.getColumnSpan() - 1;
    int l = paramCellElement2.getColumn();
    int i1 = paramCellElement2.getColumn() + paramCellElement2.getColumnSpan() - 1;
    if (k == i1)
    {
      if (j <= l)
        return;
      setDefaultUpParent(ColumnRow.valueOf(j, i), paramReport, l, j - 1);
    }
    else if (k < l)
    {
      setDefaultUpParent(ColumnRow.valueOf(j, i), paramReport, k + 1, l);
    }
    else if (i1 < j)
    {
      setDefaultUpParent(ColumnRow.valueOf(j, i), paramReport, i1, j - 1);
    }
  }

  private static void setDefaultLeftParent(ColumnRow paramColumnRow, Report paramReport, int paramInt1, int paramInt2)
  {
    Iterator localIterator = paramReport.cellIterator();
    while (true)
    {
      CellElement localCellElement;
      CellExpandAttr localCellExpandAttr;
      do
      {
        Object localObject;
        do
        {
          int i;
          do
          {
            if (!(localIterator.hasNext()))
              return;
            localCellElement = (CellElement)localIterator.next();
            i = localCellElement.getRow();
          }
          while ((i < paramInt1) || (i > paramInt2));
          localObject = localCellElement.getValue();
        }
        while (localObject instanceof DSColumn);
        localCellExpandAttr = localCellElement.getCellExpandAttr();
      }
      while ((!(localCellExpandAttr.isLeftParentDefault())) || (localCellExpandAttr.getLeftParentColumnRow() != null) || (willLeadToDeathCircle(paramReport, paramColumnRow, ColumnRow.valueOf(localCellElement.getColumn(), localCellElement.getRow()), new HashSet())));
      localCellExpandAttr.setLeftParentColumnRow(paramColumnRow);
    }
  }

  private static void setDefaultUpParent(ColumnRow paramColumnRow, Report paramReport, int paramInt1, int paramInt2)
  {
    Iterator localIterator = paramReport.cellIterator();
    while (true)
    {
      CellElement localCellElement;
      CellExpandAttr localCellExpandAttr;
      do
      {
        Object localObject;
        do
        {
          int i;
          do
          {
            if (!(localIterator.hasNext()))
              return;
            localCellElement = (CellElement)localIterator.next();
            i = localCellElement.getColumn();
          }
          while ((i < paramInt1) || (i > paramInt2));
          localObject = localCellElement.getValue();
        }
        while (localObject instanceof DSColumn);
        localCellExpandAttr = localCellElement.getCellExpandAttr();
      }
      while ((!(localCellExpandAttr.isUpParentDefault())) || (localCellExpandAttr.getUpParentColumnRow() != null) || (willLeadToDeathCircle(paramReport, paramColumnRow, ColumnRow.valueOf(localCellElement.getColumn(), localCellElement.getRow()), new HashSet())));
      localCellExpandAttr.setUpParentColumnRow(paramColumnRow);
    }
  }

  private static boolean willLeadToDeathCircle(Report paramReport, ColumnRow paramColumnRow1, ColumnRow paramColumnRow2, Set paramSet)
  {
    if ((paramColumnRow1 == null) || (paramColumnRow2 == null))
      return false;
    CellElement localCellElement = paramReport.getCellElement(paramColumnRow1.column, paramColumnRow1.row);
    if (localCellElement == null)
      return false;
    if (localCellElement.getCellExpandAttr() != null)
    {
      CellExpandAttr localCellExpandAttr = localCellElement.getCellExpandAttr();
      ColumnRow localColumnRow1 = localCellExpandAttr.getLeftParentColumnRow();
      ColumnRow localColumnRow2 = localCellExpandAttr.getUpParentColumnRow();
      if ((paramSet.contains(localColumnRow1)) || (paramSet.contains(localColumnRow2)))
        return true;
      if ((paramColumnRow2.equals(localCellExpandAttr.getLeftParentColumnRow())) || (paramColumnRow2.equals(localCellExpandAttr.getUpParentColumnRow())))
        return true;
      if (localColumnRow1 != null)
        paramSet.add(localColumnRow1);
      if (localColumnRow2 != null)
        paramSet.add(localColumnRow2);
      return ((willLeadToDeathCircle(paramReport, localCellExpandAttr.getLeftParentColumnRow(), paramColumnRow2, paramSet)) && (willLeadToDeathCircle(paramReport, localCellExpandAttr.getUpParentColumnRow(), paramColumnRow2, paramSet)));
    }
    return false;
  }
}